<?php
include('/home/wwwroot/football.laulei.com/protected/extensions/simple_html_dom.php');
//获取并更新北京单场当前期的亚盘数据
//截止时间是比赛前15分钟
class GetOddsInfosAsiaCommand extends CConsoleCommand  
{
    public function run($args) {
    	$now=date('Y-m-d H:i:s',strtotime("-25 minute"));
    	//$now=date('Y-m-d H:i:s',strtotime("-1825 day"));
    	$lstGameInfo = GameInfo::model()->findAllByAttributes(
		        array(),
		        $condition  = 'gametime >= :gameTime',
		        $params     = array(
		                ':gameTime' => $now,
		        )
		);
		foreach ($lstGameInfo as $key => $value) {
			$oddsUrl=sprintf("http://odds.500.com/fenxi/yazhi-%s-show-2", $value->gameid);
			printf("%s\n", $oddsUrl);
			$htmlOdds = file_get_html($oddsUrl);
	    	if(!isset($htmlOdds)){
	    		continue;
	    	}
	    	$oddshomemacau=0;
    		$ratemacau=0;
    		$oddsawaymacau=0;
	    	foreach($htmlOdds->find('div[class=M_content] tr[id]') as $element){
	    		$companyid=trim($element->id);
	    		$companyname=$element->find("td", 1)->plaintext;
	    		$companyname=trim(iconv("GB2312","utf-8//TRANSLIT//IGNORE",$companyname));
	    		$oddshome=trim($element->find("td", 2)->plaintext);
	    		$rate=trim($element->find("td", 3)->ref);
	    		$oddsaway=trim($element->find("td", 4)->plaintext);
	    		printf("[gameid=%s] [companyid=%s] [companyname=%s] [oddshome=%s] [rate=%s] [oddsaway=%s]\n", $value->gameid, $companyid, $companyname, $oddshome, $rate, $oddsaway);
	    		$oddsInfoAsia=new OddsInfoAsia;
				$oddsInfoAsia->gameid=$value->gameid;
				$oddsInfoAsia->companyid=$companyid;
				$oddsInfoAsia->companyname=$companyname;
				$oddsInfoAsia->oddshome=$oddshome;
				$oddsInfoAsia->oddsaway=$oddsaway;
				$oddsInfoAsia->rate=$rate;
				$oddsInfoAsia->writetime=date("Y-m-d H:i:s");
				$oddsInfoAsia->save();
				printf("inserted\n");

				if($companyid=="5"){
					$oddshomemacau=$oddshome;
		    		$ratemacau=$rate;
		    		$oddsawaymacau=$oddsaway;
				}
	    	}
	    	//平均值
	    	$companyid='-1';
	    	$companyname='平均值';
	    	$oddshome=trim($htmlOdds->find('td[id=avges1]', 0)->plaintext);
    		$rate=trim($htmlOdds->find('td[id=avgeh]', 0)->plaintext);
    		$oddsaway=trim($htmlOdds->find('td[id=avges2]', 0)->plaintext);
    		printf("[gameid=%s] [companyid=%s] [companyname=%s] [oddshome=%s] [rate=%s] [oddsaway=%s]\n", $value->gameid, $companyid, $companyname, $oddshome, $rate, $oddsaway);
	    	$oddsInfoAsia=new OddsInfoAsia;
			$oddsInfoAsia->gameid=$value->gameid;
			$oddsInfoAsia->companyid=$companyid;
			$oddsInfoAsia->companyname=$companyname;
			$oddsInfoAsia->oddshome=$oddshome;
			$oddsInfoAsia->oddsaway=$oddsaway;
			$oddsInfoAsia->rate=$rate;
			$oddsInfoAsia->writetime=date("Y-m-d H:i:s");
			$oddsInfoAsia->save();
	    	$gameInfoObj=GameInfo::model()->find(array('condition'=>'issue=:issue and gameindex=:gameindex','params'=>array(':issue'=>$value->issue, ':gameindex'=>$value->gameindex)));
			if (isset($gameInfoObj)){
				$gameInfoObj->oddshome=$oddshome;
				$gameInfoObj->rate=$rate;
				$gameInfoObj->oddsaway=$oddsaway;
				$gameInfoObj->oddshomemacau=$oddshomemacau;
				$gameInfoObj->ratemacau=$ratemacau;
				$gameInfoObj->oddsawaymacau=$oddsawaymacau;
				$gameInfoObj->update();
				printf("updated\n");
			}
	    	/*
	    	foreach ($betCompany as $key2 => $value2) {
	    		$tmpTR=$htmlOdds->find(sprintf("tr[id=tr2_%s]", $key2), 0);
	    		if(!isset($tmpTR)){
	    			continue;
	    		}
	    		$odds3=$tmpTR->find("td", 0)->plaintext;
	    		$odds1=$tmpTR->find("td", 1)->plaintext;
	    		$odds0=$tmpTR->find("td", 2)->plaintext;
	    		$oddsInfo=new OddsInfo;
				$oddsInfo->gameid=$value->gameid;
				$oddsInfo->companyid=$key2;
				$oddsInfo->companyname=$value2;
				$oddsInfo->odds3=$odds3;
				$oddsInfo->odds1=$odds1;
				$oddsInfo->odds0=$odds0;
				array_push($discreteValueArray, 
	    			array(
	    			'Odds3' => $odds3,
	    			'Odds1' => $odds1,
	    			'Odds0' => $odds0,
	    			)
	    		);
	    		$avgOdds3z+=$odds3;
	    		$avgOdds1z+=$odds1;
	    		$avgOdds0z+=$odds0;
				if($key2=='293'){
					//威廉希尔，计算概率
					$gameInfoList=GameInfo::model()->findAll(
						array(
							'condition'=>'euroodds3w=:euroodds3w and euroodds1w=:euroodds1w and euroodds0w=:euroodds0w and homegoal!=-1 and awaygoal!=-1 and gametime<:gametime',
							'params'=>array(
								':euroodds3w'=>$odds3,
								':euroodds1w'=>$odds1,
								':euroodds0w'=>$odds0,
								':gametime'=>$value->gametime,
								)
							)
						);
					if(isset($gameInfoList)){
						$gameCount=0;
						$winCount=0;
						$drawCount=0;
						$loseCount=0;
						$winprobability=0;
						$drawprobability=0;
						$loseprobability=0;
						foreach ($gameInfoList as $listKey => $listValue) {
							if($listValue->homegoal!=-1){
								$gameCount++;
								if($listValue->homegoal>$listValue->awaygoal){
									$winCount++;
								}
								else if($listValue->homegoal==$listValue->awaygoal){
									$drawCount++;
								}
								else if($listValue->homegoal<$listValue->awaygoal){
									$loseCount++;
								}
							}
						}
						if($gameCount>0){
							$oddsInfo->winprobability=$winprobability=$winCount/$gameCount;
							$oddsInfo->drawprobability=$drawprobability=$drawCount/$gameCount;
							$oddsInfo->loseprobability=$loseprobability=$loseCount/$gameCount;
						}
						$gameInfoObj=GameInfo::model()->find(
								array(
									'condition'=>'gameid=:gameid',
									'params'=>array(
										':gameid'=>$value->gameid,
										)
									)
								);
						if(isset($gameInfoObj)){
							$gameInfoObj->euroodds3w=$odds3;
							$gameInfoObj->euroodds1w=$odds1;
							$gameInfoObj->euroodds0w=$odds0;
							$gameInfoObj->winprobability=$winprobability;
							$gameInfoObj->drawprobability=$drawprobability;
							$gameInfoObj->loseprobability=$loseprobability;
							if($gameInfoObj->update()){
								printf("$value2 [编号：%s] [威廉希尔胜赔：%s] [威廉希尔平赔：%s] [威廉希尔负赔：%s] [概率胜：%.2f] [概率平：%.2f] [概率负：%.2f]\n", $value->gameindex, $odds3,$odds1,$odds0,$winprobability,$drawprobability,$loseprobability);
							}
							else{
								printf("更新威廉希尔赔率失败 [编号：%s] [威廉希尔胜赔：%s] [威廉希尔平赔：%s] [威廉希尔负赔：%s] [概率胜：%.2f] [概率平：%.2f] [概率负：%.2f]\n", $value->gameindex, $odds3,$odds1,$odds0,$winprobability,$drawprobability,$loseprobability);
							}
						}
					}

					//威廉希尔，计算概率（同比赛类型）
					$gameInfoList=GameInfo::model()->findAll(
						array(
							'condition'=>'euroodds3w=:euroodds3w and euroodds1w=:euroodds1w and euroodds0w=:euroodds0w and homegoal!=-1 and awaygoal!=-1 and gametype=:gametype and gametime<:gametime',
							'params'=>array(
								':euroodds3w'=>$odds3,
								':euroodds1w'=>$odds1,
								':euroodds0w'=>$odds0,
								':gametype'=>$gametype,
								':gametime'=>$value->gametime,
								)
							)
						);
					if(isset($gameInfoList)){
						$gameCount=0;
						$winCount=0;
						$drawCount=0;
						$loseCount=0;
						$winprobability=0;
						$drawprobability=0;
						$loseprobability=0;
						foreach ($gameInfoList as $listKey => $listValue) {
							if($listValue->homegoal!=-1){
								$gameCount++;
								if($listValue->homegoal>$listValue->awaygoal){
									$winCount++;
								}
								else if($listValue->homegoal==$listValue->awaygoal){
									$drawCount++;
								}
								else if($listValue->homegoal<$listValue->awaygoal){
									$loseCount++;
								}
							}
						}
						if($gameCount>0){
							$winprobability=$winCount/$gameCount;
							$drawprobability=$drawCount/$gameCount;
							$loseprobability=$loseCount/$gameCount;
						}
						$gameInfoObj=GameInfo::model()->find(
								array(
									'condition'=>'gameid=:gameid',
									'params'=>array(
										':gameid'=>$value->gameid,
										)
									)
								);
						if(isset($gameInfoObj)){
							$gameInfoObj->winprobability_samegametype=$winprobability;
							$gameInfoObj->drawprobability_samegametype=$drawprobability;
							$gameInfoObj->loseprobability_samegametype=$loseprobability;
							if($gameInfoObj->update()){
								printf("相同比赛类型概率 $value2 [编号：%s] [威廉希尔胜赔：%s] [威廉希尔平赔：%s] [威廉希尔负赔：%s] [概率胜：%.2f] [概率平：%.2f] [概率负：%.2f]\n", $value->gameindex, $odds3,$odds1,$odds0,$winprobability,$drawprobability,$loseprobability);
							}
							else{
								printf("更新相同比赛类型概率失败 [编号：%s] [威廉希尔胜赔：%s] [威廉希尔平赔：%s] [威廉希尔负赔：%s] [概率胜：%.2f] [概率平：%.2f] [概率负：%.2f]\n", $value->gameindex, $odds3,$odds1,$odds0,$winprobability,$drawprobability,$loseprobability);
							}
						}
					}
				}
				$oddsInfo->writetime=date("Y-m-d H:i:s");
				$oddsInfo->save();
				printf("$value2 [编号：%s] [胜赔：%s] [平赔：%s] [负赔：%s]\n", $value->gameindex, $odds3,$odds1,$odds0);
	    	}
	    	*/
	    	$htmlOdds->clear();
			unset($htmlOdds);
		}
    }
}